using System.Collections.Generic;
using System.Linq;
namespace HB.Expand
{
    /// <summary>
    /// 数组拓展
    /// </summary>
    public static class ArrayExpand
    {
        /// <summary>
        /// 返回指定索引的数据，若超出索引，则返回默认值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="index"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T TryGet<T>(this T[] data, int index, T defaultValue = default(T))
        {
            if (data == null) { return defaultValue; }
            if (data.Length == 0) { return defaultValue; }
            if (index.BetweenIn(0, data.Length - 1))
            {
                return data[index];
            }
            return defaultValue;

        }
        /// <summary>
        /// 返回指定索引的数据，若超出索引，则返回默认索引的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="index"></param>
        /// <param name="defaultIndex"></param>
        /// <returns></returns>
        public static T TryGet<T>(this T[] data, int index, int defaultIndex = -1)
        {
            if (data == null) { return default(T); }
            if (data.Length == 0) { return default(T); }
            if (index.BetweenIn(0, data.Length - 1))
            {
                return data[index];
            }
            else
            {
                if (defaultIndex != -1)
                {
                    if (defaultIndex.BetweenIn(0, data.Length))
                    {
                        return data[defaultIndex];
                    }
                }
                return default(T);
            }
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="conn"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<T> TakePage<T>(this ICollection<T> conn, int page, int pageSize)
        {
            int pagenow = page < 1 ? 1 : page;
            int offset = pageSize * (page - 1);
            return conn.Skip(offset).Take(pageSize).ToList();
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="conn"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<T> TakePage<T>(this IEnumerable<T> conn, int page, int pageSize)
        {
            int pagenow = page < 1 ? 1 : page;
            int offset = pageSize * (page - 1);
            return conn.Skip(offset).Take(pageSize).ToList();
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="conn"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<T> TakePage<T>(this IList<T> conn, int page, int pageSize)
        {
            int pagenow = page < 1 ? 1 : page;
            int offset = pageSize * (page - 1);
            return conn.Skip(offset).Take(pageSize).ToList();
        }


        /// <summary>
        /// 通过字符串将数组连接起来
        /// </summary>
        /// <param name="strs"></param>
        /// <param name="spiltor">分隔符</param>
        /// <returns></returns>
        public static string JoinBySpiltor(this IEnumerable<string> strs, string spiltor)
        {
            return string.Join(spiltor, strs);
        }
    }
}
